package com.yy.micro.magic.exception;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class ExceptionManager {

    private List<MapException> exceptions = new ArrayList<>();

//    private Map<String, Set<MapException>> exceptions;
    private ExceptionCategory category = ExceptionCategory.RUNTIME;

//    public ExceptionManager() {
//        exceptions = new HashMap<String, Set<MapException>>();
//    }


    public void setExceptions(final List<MapException> exceptions) {
        this.exceptions = exceptions;
    }

    public void addException(String strategy,MapException exception){
        if (null != exception){
            exception.setStrategy(strategy);
            addException(exception);
        }
    }

    public void addException(MapException exception){
        if(null != exception){
            exceptions.add(exception);
            merge(exception.getCategory());
        }
    }

//
//
//    public void addException(String name, String message) {
//        addException(name, message, null);
//    }
//
//    public void addException(String name, String message, Throwable throwable) {
//        addException(name, message, throwable, ExceptionCategory.RUNTIME);
//    }
//
//    public void addException(String name, Throwable throwable) {
//        addException(name, null, throwable);
//    }
//
//    public void addException(String name, String message, Throwable throwable, ExceptionCategory category) {
//        Set<MapException> es = exceptions.get(name);
//        if (null == es || es.isEmpty()) {
//            es = new HashSet<MapException>();
//            exceptions.put(name, es);
//        }
//        MapException e = new MapException(category,message);
//        if (null != throwable){
//            e = new MapException(message,throwable);
//        }
//        es.add(e);
//    }
//
//    public String getMessages() {
//        final StringBuilder sb = new StringBuilder();
//        if (null != exceptions) {
//            sb.append(String.format("Exception Class size [%d] ", exceptions.size()));
//            for (final Map.Entry<String, Set<MapException>> entry : exceptions.entrySet()) {
//                int index = 0;
//                final Set<MapException> es = entry.getValue();
//                if (null != es && !es.isEmpty()) {
//                    final StringBuilder str = new StringBuilder();
//                    str.append(String.format(",Class [ %d:%s ], Warn [ %d ]: {", index++,
//                            entry.getKey(), es.size()));
//                    int num = 0;
//                    for (final MapException exception : entry.getValue()) {
//                        str.append(String.format("Exception[%d] = %s , ", num++, exception.getMessage()));
//                    }
//                    str.append("};");
//                    sb.append(str);
//                }
//            }
//        }
//        return sb.toString();
//    }

    private void merge(ExceptionCategory category){
        if(null != category){
            if (category.getCode() > this.category.getCode()){
                this.category = category;
            }
        }
    }

    public boolean isBlock() {
        return !CollectionUtils.isEmpty(exceptions) &&this.category.isBlock();
    }
}
